package com.lemon.smartsafetynew.ui.fragment.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.ldzs.recyclerlibrary.Mode;
import com.ldzs.recyclerlibrary.PullToRefreshRecyclerView;
import com.ldzs.recyclerlibrary.anim.SlideInLeftAnimator;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.listener.PullRefreshListener;
import com.lemon.smartsafety.view.list.MultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

public abstract class BaseLocalListFragment<E> extends BaseFragment implements PullRefreshListener {

    @Bind(R.id.pullToRefreshRecyclerView)
    PullToRefreshRecyclerView mPullToRefreshRecyclerView;


    @Bind(R.id.multiStateView)
    public MultiStateView mMultiStateView;

    protected LemonRcvAdapter<E> mAdapter;

    public List<E> mDataList = new ArrayList<>();
    @Override
    protected void initViews() {
        if (mMultiStateView != null) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        }
        mPullToRefreshRecyclerView.setItemAnimator(new SlideInLeftAnimator());
        mPullToRefreshRecyclerView.getItemAnimator().setAddDuration(Constants.RECYCLE_VIEW_ANIMATOR_DURATION);
        mPullToRefreshRecyclerView.getItemAnimator().setRemoveDuration(Constants.RECYCLE_VIEW_ANIMATOR_DURATION);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mPullToRefreshRecyclerView.setRefreshMode(Mode.DISABLED);
        mPullToRefreshRecyclerView.setLayoutManager(layoutManager);
        mAdapter = getLemonAdapter();
        mPullToRefreshRecyclerView.setAdapter(mAdapter);
        mPullToRefreshRecyclerView.setOnPullUpToRefreshListener(new PullToRefreshRecyclerView.OnPullUpToRefreshListener() {
            @Override
            public void onRefresh() {
                onPullRefresh();
            }
        });
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (goRefreshOnCreate()) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            mPullToRefreshRecyclerView.beginRefreshing();
        }
    }



    /**
     * 是否在视图创建好后，就自动下来刷新
     *
     * @return
     */
    public abstract boolean goRefreshOnCreate();


    @Override
    protected void getBaseArguments() {

    }

    /**
     * 设置页面的Adapter
     * @return
     */
    public abstract LemonRcvAdapter<E> getLemonAdapter();


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_refresh_no_title;
    }


}
